package com.solome.map;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;
import com.solome.base.AbstractScreen;
import com.solome.map.LoadMap.MapData;
import com.solome.map.LoadMap.MapInfo;

/**
 * @author soells
 * @E-mail:soells@foxmail.com
 * @version 创建时间：2014年9月15日 下午9:16:54
 * @DES 程序的简单说明
 */
public class MapReader extends AbstractScreen {
	public MapReader(Game game) {
		super(game);
		// TODO Auto-generated constructor stub
	}

	Map<String, Texture> textureDicts = new ConcurrentHashMap<String, Texture>();

	public Texture getTexture(String name) {
		if (textureDicts.containsKey(name)) {
			return textureDicts.get(name);
		}

		Texture texture = new Texture(Gdx.files.internal(PATH
				+ String.valueOf(name) + ".png"));
		textureDicts.put(name, texture);
		return texture;
	}

	// this thisBack;

	TextureRegion background;
	LoadMap loadFile;
	float curX, curY, width, height;

	int FILE = 40003;
	final String PATH = String.format("source/map/m_%d/", FILE);

	private OrthoCamController cameraController;

	public List<Integer> runData = new CopyOnWriteArrayList<Integer>();// 可行走

	@Override
	public void init() {
		BitmapFont bitmapFont = new BitmapFont();
		float w = this.getWidth();// Gdx.graphics.getWidth();
		float h = this.getHeight();// Gdx.graphics.getHeight();
		width = w;
		height = h;
		this.setDebugGameInfo(true);
		loadFile = new LoadMap();
		loadFile.openMapData(PATH + FILE + ".dat");
		
		
		
		
		

		background = new TextureRegion(new Texture(Gdx.files.internal(PATH
				+ (String.valueOf((100000 + loadFile.backID))) + ".png")));
		// Image backgroundImage = new Image(background);
		// backgroundImage.setX(0);
		// backgroundImage.setY(0);
		// backgroundImage.setFillParent(true);
		// this.add(backgroundImage);
		{
			Texture tiles = new Texture(Gdx.files.internal(PATH
					+ String.valueOf(loadFile.tileID) + ".png"));
			TextureRegion[][] splitTiles = TextureRegion.split(tiles,
					loadFile.colW, loadFile.colH);
			for (MapInfo mapInfo : loadFile.map3Dicts) {
				Texture texture = getTexture("" + mapInfo.tempName);
				// new Texture(Gdx.files.internal(PATH
				// + String.valueOf(mapInfo.tempName) + ".png"));

				TextureRegion textureRegion = new TextureRegion(texture);
				Image image = new Image(textureRegion);
				image.setX(mapInfo.tempX);
				image.setY(loadFile.mapH - mapInfo.tempY - image.getHeight());
				this.add(image);
			}

			for (MapInfo mapInfo : loadFile.map1Dicts) {
				Texture texture = getTexture("" + mapInfo.tempName);
				TextureRegion textureRegion = new TextureRegion(texture);
				Image image = new Image(textureRegion);
				image.setX(mapInfo.tempX);
				image.setY(loadFile.mapH - mapInfo.tempY - image.getHeight());
				this.add(image);
			}

			for (MapInfo mapInfo : loadFile.map2Dicts) {
				Texture texture = getTexture("" + mapInfo.tempName);
				TextureRegion textureRegion = new TextureRegion(texture);
				Image image = new Image(textureRegion);
				image.setX(mapInfo.tempX);
				image.setY(loadFile.mapH - mapInfo.tempY - image.getHeight());
				this.add(image);
			}

			for (MapInfo mapInfo : loadFile.map4Dicts) {
				Texture texture = getTexture("" + mapInfo.tempName);
				TextureRegion textureRegion = new TextureRegion(texture);
				Image image = new Image(textureRegion);
				image.setX(mapInfo.tempX);
				image.setY(loadFile.mapH - mapInfo.tempY - image.getHeight());
				this.add(image);
			}

			for (int i = 0; i < loadFile.roadData.size(); i++) {
				MapData mapData = loadFile.roadData.get(i);
				if (mapData.tempByte < 0) {
					continue;
				}

				// if (mapData.type != 0 || mapData.mType != 0
				// || mapData.fType != 0) {
				// continue;
				// }
				short mj_tileX = (short) ((i / loadFile.rowNum) * loadFile.colW);
				short mj_tileY = (short) (loadFile.mapH - loadFile.colH - (i % loadFile.rowNum)
						* loadFile.colH);
				Image image = new Image(splitTiles[0][mapData.tempByte]);
				image.setX(mj_tileX);
				image.setY(mj_tileY);
				mapData.x = mj_tileX;
				mapData.y = mj_tileY;
				image.setName("" + i);
				this.add(image);
				runData.add(i);
			}

			for (Integer integer : runData) {
				short mj_tileX = (short) ((integer.intValue() / loadFile.rowNum) * loadFile.colW);
				short mj_tileY = (short) (loadFile.mapH - loadFile.colH - (integer
						.intValue() % loadFile.rowNum) * loadFile.colH);

				LabelStyle labelStyle = new LabelStyle(bitmapFont, Color.YELLOW); // 创建一个Label样式，使用默认
				Label label = new Label(integer.toString(), labelStyle); // 创建标签，显示的文字是FPS：
				label.setName(integer.toString()); // 设置标签名称为fpsLabel
				label.setX(mj_tileX + (loadFile.colW - label.getWidth()) / 2); // 设置X值，
				label.setY(mj_tileY + (loadFile.colH - label.getHeight()) / 2); // 设置Y
				this.add(label); // 将标签添加到舞台
			}

			cameraController = new OrthoCamController(this.getCamera());
			Gdx.input.setInputProcessor(cameraController);

			// layers.add(layer);
		}
		// renderer = new OrthogonalTiledMapRenderer(map);
	}

	@Override
	public void resize(int width, int height) {
		// TODO Auto-generated method stub

	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub

	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub

	}

	@Override
	public void draw(float delta) {
		// TODO Auto-generated method stub

	}

	@Override
	public void free() {
		// TODO Auto-generated method stub
		background.getTexture().dispose();
		for (Texture texture : textureDicts.values()) {
			texture.dispose();
		}
		textureDicts.clear();
		System.out.println("移除资源。。。。");
	}
}
